package com.bianmaba.example.eacache;

import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Status;
import net.sf.ehcache.event.CacheManagerEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @program: example-web-framework-extends
 * @description:
 * @author: Chenjiabin
 * @create: 2018-04-21 18:16
 **/
public class DefaultCacheManagerEventListener implements CacheManagerEventListener {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private final CacheManager cacheManager;

    protected boolean enabled;

    public DefaultCacheManagerEventListener(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

    public DefaultCacheManagerEventListener(CacheManager cacheManager, boolean enabled) {
        this.cacheManager = cacheManager;
        this.enabled = enabled;
    }

    @Override
    public void init() throws CacheException {
        if (enabled) {
            logger.info("初始化缓存！");
        }
    }

    @Override
    public Status getStatus() {
        return cacheManager.getStatus();
    }

    @Override
    public void dispose() throws CacheException {
        if (enabled) {
            logger.info("关闭缓存！");
        }
    }

    @Override
    public void notifyCacheAdded(String name) {
        if (enabled) {
            logger.info("创建缓存:{}", name);
        }
    }

    @Override
    public void notifyCacheRemoved(String name) {
        if (enabled) {
            logger.info("删除缓存:{}", name);
        }
    }
}